package mark.tom.index.compiler.utils;

import javax.lang.model.element.Element;

import mark.tom.index.facade.enums.TypeKind;

public class StatementUtil {

    public static String buildRestoreInjectMethodStatement(String statement, TypeUtils typeUtils, Element element, String fieldName, String bundleKey) {
        String extraStatement = null;
        int type = typeUtils.typeExchange(element);
        if (type == TypeKind.BOOLEAN_ARRAY.ordinal()) {
            // boolean[]
            statement += "getBooleanArray($S)";
        } else if (type == TypeKind.BOOLEAN.ordinal()) {
            // boolean || java.lang.Boolean
            statement += "getBoolean($S)";
            if (!element.asType().getKind().isPrimitive()) {
                extraStatement = "else if (nonValueKey.contains(\"" + bundleKey + "\")) {substitute." + fieldName + " = null;}";
            }
        } else if (type == TypeKind.BYTE_ARRAY.ordinal()) {
            // byte[]
            statement += "getByteArray($S)";
        } else if (type == TypeKind.BYTE.ordinal()) {
            // byte || java.lang.Byte
            statement += "getByte($S)";
            if (!element.asType().getKind().isPrimitive()) {
                extraStatement = "else if (nonValueKey.contains(\"" + bundleKey + "\")) {substitute." + fieldName + " = null;}";
            }
        } else if (type == TypeKind.CHAR_ARRAY.ordinal()) {
            // char[]
            statement += "getCharArray($S)";
        } else if (type == TypeKind.CHAR.ordinal()) {
            // char
            statement += "getChar($S)";
        } else if (type == TypeKind.CHARSEQUENCE_ARRAY.ordinal()) {
            // CharSequence[]
            statement += "getCharSequenceArray($S)";
        } else if (type == TypeKind.ARRAY_LIST_CHARSEQUENCE.ordinal()) {
            // ArrayList<CharSequence>
            statement += "getCharSequenceArrayList($S)";
        } else if (type == TypeKind.CHARSEQUENCE.ordinal()) {
            // CharSequence
            statement += "getCharSequence($S)";
        } else if (type == TypeKind.DOUBLE_ARRAY.ordinal()) {
            // double[]
            statement += "getDoubleArray($S)";
        } else if (type == TypeKind.DOUBLE.ordinal()) {
            // double || java.lang.Double
            statement += "getDouble($S)";
            if (!element.asType().getKind().isPrimitive()) {
                extraStatement = "else if (nonValueKey.contains(\"" + bundleKey + "\")) {substitute." + fieldName + " = null;}";
            }
        } else if (type == TypeKind.FLOAT_ARRAY.ordinal()) {
            // float[]
            statement += "getFloatArray($S)";
        } else if (type == TypeKind.FLOAT.ordinal()) {
            // float || Float
            statement += "getFloat($S)";
            if (!element.asType().getKind().isPrimitive()) {
                extraStatement = "else if (nonValueKey.contains(\"" + bundleKey + "\")) {substitute." + fieldName + " = null;}";
            }
        } else if (type == TypeKind.INT_ARRAY.ordinal()) {
            // int[]
            statement += "getIntArray($S)";
        } else if (type == TypeKind.ARRAY_LIST_INTEGER.ordinal()) {
            // ArrayList<Integer>
            statement += "getIntegerArrayList($S)";
        } else if (type == TypeKind.INT.ordinal()) {
            // int || java.lang.Integer
            statement += "getInt($S)";
            if (!element.asType().getKind().isPrimitive()) {
                extraStatement = "else if (nonValueKey.contains(\"" + bundleKey + "\")) {substitute." + fieldName + " = null;}";
            }
        } else if (type == TypeKind.LONG_ARRAY.ordinal()) {
            // long[]
            statement += "getLongArray($S)";
        } else if (type == TypeKind.LONG.ordinal()) {
            // long || java.lang.Long
            statement += "getLong($S)";
            if (!element.asType().getKind().isPrimitive()) {
                extraStatement = "else if (nonValueKey.contains(\"" + bundleKey + "\")) {substitute." + fieldName + " = null;}";
            }
        } else if (type == TypeKind.SHORT_ARRAY.ordinal()) {
            // short[]
            statement += "getShortArray($S)";
        } else if (type == TypeKind.SHORT.ordinal()) {
            // short || java.lang.Short
            statement += "getShort($S)";
            if (!element.asType().getKind().isPrimitive()) {
                extraStatement = "else if (nonValueKey.contains(\"" + bundleKey + "\")) {substitute." + fieldName + " = null;}";
            }
        } else if (type == TypeKind.STRING_ARRAY.ordinal()) {
            // String[]
            statement += "getStringArray($S)";
        } else if (type == TypeKind.ARRAY_LIST_STRING.ordinal()) {
            // ArrayList<String>
            statement += "getStringArrayList($S)";
        } else if (type == TypeKind.STRING.ordinal()) {
            // String
            statement += "getString($S)";
        } else if (type == TypeKind.PARCELABLE.ordinal()) {
            // Parcelable
            statement += "getParcelable($S)";
        } else if (type == TypeKind.PARCELABLE_ARRAY.ordinal()) {
            // Parcelable[]
            statement += "getParcelableArray($S)";
            statement = statement.replaceAll("= ", "= (" + element.asType().toString() + ") ");
        } else if (type == TypeKind.ARRAY_LIST_PARCELABLE.ordinal()) {
            // ArrayList<Parcelable>
            statement += "getParcelableArrayList($S)";
        } else if (type == TypeKind.SERIALIZABLE.ordinal()) {
            // Serializable
            statement += "getSerializable($S)";
            statement = statement.replaceAll("= ", "= (" + element.asType().toString() + ") ");
        } else if (type == TypeKind.OBJECT.ordinal()) {
            statement = null;
        }
        if (statement != null) {
            statement = "if (savedInstanceState.containsKey" + "(\"" + bundleKey + "\")) {" + statement + ";}";
            if (extraStatement != null) {
                statement += " " + extraStatement;
            }
        }
        return statement;
    }

    public static String buildRestoreRestoreMethodStatement(String statement, TypeUtils typeUtils, Element element, String fieldName, String bundleKey) {
        String extraStatement = null;
        int type = typeUtils.typeExchange(element);
        if (type == TypeKind.BOOLEAN_ARRAY.ordinal()) {
            // boolean[]
            statement += "putBooleanArray($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.BOOLEAN.ordinal()) {
            // boolean || java.lang.Boolean
            statement += "putBoolean($S, substitute." + fieldName + ")";
            if (!element.asType().getKind().isPrimitive()) {
                extraStatement = "if (substitute." + fieldName + " == null) { nonValueKey.add(\"" + bundleKey + "\");}";
            }
        } else if (type == TypeKind.BYTE_ARRAY.ordinal()) {
            // byte[]
            statement += "putByteArray($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.BYTE.ordinal()) {
            // byte || java.lang.Byte
            statement += "putByte($S, substitute." + fieldName + ")";
            if (!element.asType().getKind().isPrimitive()) {
                extraStatement = "if (substitute." + fieldName + " == null) { nonValueKey.add(\"" + bundleKey + "\");}";
            }
        } else if (type == TypeKind.CHAR_ARRAY.ordinal()) {
            // char[]
            statement += "putCharArray($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.CHAR.ordinal()) {
            // char
            statement += "putChar($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.CHARSEQUENCE_ARRAY.ordinal()) {
            // CharSequence[]
            statement += "putCharSequenceArray($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.ARRAY_LIST_CHARSEQUENCE.ordinal()) {
            // ArrayList<CharSequence>
            statement += "putCharSequenceArrayList($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.CHARSEQUENCE.ordinal()) {
            // CharSequence
            statement += "putCharSequence($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.DOUBLE_ARRAY.ordinal()) {
            // double[]
            statement += "putDoubleArray($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.DOUBLE.ordinal()) {
            // double || java.lang.Double
            statement += "putDouble($S, substitute." + fieldName + ")";
            if (!element.asType().getKind().isPrimitive()) {
                extraStatement = "if (substitute." + fieldName + " == null) { nonValueKey.add(\"" + bundleKey + "\");}";
            }
        } else if (type == TypeKind.FLOAT_ARRAY.ordinal()) {
            // float[]
            statement += "putFloatArray($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.FLOAT.ordinal()) {
            // float || Float
            statement += "putFloat($S, substitute." + fieldName + ")";
            if (!element.asType().getKind().isPrimitive()) {
                extraStatement = "if (substitute." + fieldName + " == null) { nonValueKey.add(\"" + bundleKey + "\");}";
            }
        } else if (type == TypeKind.INT_ARRAY.ordinal()) {
            // int[]
            statement += "putIntArray($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.ARRAY_LIST_INTEGER.ordinal()) {
            // ArrayList<Integer>
            statement += "putIntegerArrayList($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.INT.ordinal()) {
            // int || java.lang.Integer
            statement += "putInt($S, substitute." + fieldName + ")";
            if (!element.asType().getKind().isPrimitive()) {
                extraStatement = "if (substitute." + fieldName + " == null) { nonValueKey.add(\"" + bundleKey + "\");}";
            }
        } else if (type == TypeKind.LONG_ARRAY.ordinal()) {
            // long[]
            statement += "putLongArray($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.LONG.ordinal()) {
            // long || java.lang.Long
            statement += "putLong($S, substitute." + fieldName + ")";
            if (!element.asType().getKind().isPrimitive()) {
                extraStatement = "if (substitute." + fieldName + " == null) { nonValueKey.add(\"" + bundleKey + "\");}";
            }
        } else if (type == TypeKind.SHORT_ARRAY.ordinal()) {
            // short[]
            statement += "putShortArray($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.SHORT.ordinal()) {
            // short || java.lang.Short
            statement += "putShort($S, substitute." + fieldName + ")";
            if (!element.asType().getKind().isPrimitive()) {
                extraStatement = "if (substitute." + fieldName + " == null) { nonValueKey.add(\"" + bundleKey + "\");}";
            }
        } else if (type == TypeKind.STRING_ARRAY.ordinal()) {
            // String[]
            statement += "putStringArray($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.ARRAY_LIST_STRING.ordinal()) {
            // ArrayList<String>
            statement += "putStringArrayList($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.STRING.ordinal()) {
            // String
            statement += "putString($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.PARCELABLE.ordinal()) {
            // Parcelable
            statement += "putParcelable($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.PARCELABLE_ARRAY.ordinal()) {
            // Parcelable[]
            statement += "putParcelableArray($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.ARRAY_LIST_PARCELABLE.ordinal()) {
            // ArrayList<Parcelable>
            statement += "putParcelableArrayList($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.SERIALIZABLE.ordinal()) {
            // Serializable
            statement += "putSerializable($S, substitute." + fieldName + ")";
        } else if (type == TypeKind.OBJECT.ordinal()) {
            statement = null;
        }
        if (statement != null && extraStatement != null) {
            statement = extraStatement + " else {" + statement + ";}";
        }
        return statement;
    }

}
